package com.example.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.Html;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import cn.smssdk.EventHandler;
import cn.smssdk.SMSSDK;

import com.example.consts.Consts;
import com.example.dianping.R;
import com.example.entity.ResponseObject;
import com.example.entity.User;
import com.example.utils.MyUtils;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;

public class MyRegisterActivity extends Activity {

	@ViewInject(R.id.btn_get_vilid_code)
	Button btn_get_vilid_code;
	@ViewInject(R.id.btn_reg)
	Button btn_reg;

	@ViewInject(R.id.et_username)
	EditText et_username;
	@ViewInject(R.id.et_vilid_code)
	EditText et_vilid_code;
	@ViewInject(R.id.et_pwd)
	EditText et_pwd;

	@ViewInject(R.id.tv_register_back)
	TextView tv_register_back;
	private CountTimer countTimer;

	private EventHandler eh;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_register);
		ViewUtils.inject(this);
		countTimer = new CountTimer(60000, 1000);
	}

	@OnClick({ R.id.btn_get_vilid_code, R.id.btn_reg })
	public void onClick(View view) {

		switch (view.getId()) {
		case R.id.btn_get_vilid_code:
			countTimer.start();
			sendSMSRandom();
			break;
		case R.id.btn_reg:
			// SMSSDK.submitVerificationCode("86", et_username.getText()
			// .toString(), et_vilid_code.getText().toString());
			registerUser();
			break;

		default:
			break;
		}

	}

	public class CountTimer extends CountDownTimer {

		public CountTimer(long millisInFuture, long countDownInterval) {
			super(millisInFuture, countDownInterval);
		}

		@Override
		public void onTick(long millisUntilFinished) {
			btn_get_vilid_code.setText(millisUntilFinished / 1000 + "后发送");
			btn_get_vilid_code.setBackgroundResource(R.drawable.buy_btn_normal);
			btn_get_vilid_code.setClickable(false);
		}

		@Override
		public void onFinish() {
			btn_get_vilid_code.setText("重新发送验证码");
			btn_get_vilid_code.setBackgroundResource(R.drawable.buy_btn_click);
			btn_get_vilid_code.setClickable(true);
		}

	}

	// Send SMS valid code
	public void sendSMSRandom() {
		SMSSDK.initSDK(this, "bdd665c56ca0", "fea7465096c88f0df998d80c012c71b4");
		System.out.println("456");
		eh = new EventHandler() {

			@Override
			public void afterEvent(int event, int result, Object data) {
				System.out.println("789");
				if (result == SMSSDK.RESULT_COMPLETE) {
					if (event == SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE) {
						System.out.println("验证码校验成功");
					} else if (event == SMSSDK.EVENT_GET_VERIFICATION_CODE) {
						System.out.println("验证码发送成功");
					} else if (event == SMSSDK.EVENT_GET_SUPPORTED_COUNTRIES) {

					}
				} else {
					((Throwable) data).printStackTrace();
				}
			}
		};
		SMSSDK.registerEventHandler(eh);
		String phoneName = et_username.getText().toString();
		SMSSDK.getVerificationCode("86", phoneName.toString());
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		if (eh != null) {
			SMSSDK.unregisterEventHandler(eh);
		}
	}

	public void registerUser() {
		if (et_username.getText().toString().trim().length() <= 0) {
			et_username.setError(Html
					.fromHtml("<font color=red>用户名不能为空</font>"));
			return;
		}

		if (et_pwd.getText().toString().trim().length() <= 0) {
			et_pwd.setFocusable(true);
			et_pwd.setFocusableInTouchMode(true);
			// 然后获取焦点：
			et_pwd.requestFocus();
			et_pwd.requestFocusFromTouch();

			et_pwd.setError(Html.fromHtml("<font color=red>密码不能为空</font>"));
			return;
		}

		if (et_vilid_code.getText().toString().trim().length() <= 0) {
			et_vilid_code.setFocusable(true);
			et_vilid_code.setFocusableInTouchMode(true);
			// 然后获取焦点：
			et_vilid_code.requestFocus();
			et_vilid_code.requestFocusFromTouch();

			et_vilid_code.setError(Html
					.fromHtml("<font color=red>验证码不能为空</font>"));
			return;
		}

		new HttpUtils().send(HttpMethod.GET, Consts.USER_REGISTER
				+ "&username=" + et_username.getText().toString().trim()
				+ "&password=" + et_pwd.getText().toString().trim(),
				new RequestCallBack<String>() {

					@Override
					public void onFailure(HttpException arg0, String arg1) {
						Toast.makeText(MyRegisterActivity.this, arg1,
								Toast.LENGTH_LONG).show();
					}

					@Override
					public void onSuccess(ResponseInfo<String> responseInfo) {
						ResponseObject<User> object = new GsonBuilder()
								.create().fromJson(responseInfo.result,
										new TypeToken<ResponseObject<User>>() {
										}.getType());
						if (object.getState() == 1) {
							Toast.makeText(MyRegisterActivity.this, "注册成功",
									Toast.LENGTH_LONG).show();
							Intent intent = new Intent(MyRegisterActivity.this,
									MyLoginActivity.class);
							intent.putExtra("login_name", et_username.getText()
									.toString());
							setResult(MyUtils.RequestRegisterCode, intent);
							finish();
						}
					}
				});
	}
}
